package top.oylan.manager.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import top.oylan.manager.entity.Section;
import top.oylan.manager.entity.example.SectionExample;
import top.oylan.manager.mapper.SectionMapper;
import top.oylan.manager.service.SectionService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author abduahad
 * @Description ${description}
 * @Date 2019-03-28
 */
@Service
public class SectionServiceImpl implements SectionService {

    @Resource
    SectionMapper sectionMapper;

    @Override
    public List<Section> selectSectionsByCondition(Section section) {
        SectionExample sectionExample = new SectionExample();
        SectionExample.Criteria criteria = sectionExample.createCriteria();
        if(section.getProductId() != null){
            criteria.andProductIdEqualTo(section.getProductId());
        }
        if(section.getParentId() != null){
           if(section.getParentId() >= 0){
               criteria.andParentIdEqualTo(section.getParentId());
           } else{
               criteria.andParentIdNotEqualTo(0);
           }
        }
        if(section.getLv() != null){
            criteria.andLvEqualTo(section.getLv());
        }
        return sectionMapper.selectByExample(sectionExample);
    }

    @Override
    public int saveSection(Section section) {
        return sectionMapper.insertSelective(section);
    }

    @Override
    public Section findSectionById(Integer id) {
        return sectionMapper.selectByPrimaryKey(id);
    }

    @Override
    public int update(Section section) {
        return sectionMapper.updateByPrimaryKeySelective(section);
    }

    @Override
    public int delete(Integer id) {
        return sectionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int saveSectionFromJSON(JSONObject jsonObject) {
        Section section = new Section();
        section.setProductId(jsonObject.getInteger("productId"));
        section.setParentId(0);
        section.setCreateTime(new Date());
        section.setTitle(jsonObject.getString("title"));
        section.setDescription(jsonObject.getString("description"));
        section.setLv((byte)1);
        section.setStatus(jsonObject.getByte("status"));
        sectionMapper.insertSelective(section);
        return section.getId().intValue();
    }

    @Override
    public void saveVideos(JSONObject jsonObject, int parentId) {
        JSONArray videos = jsonObject.getJSONArray("videos");
        for (int i = 0;i<videos.size();i++){
            JSONObject video = videos.getJSONObject(i);
            Section item = new Section();
            item.setParentId(parentId);
            item.setProductId(jsonObject.getInteger("productId"));
            item.setStatus((byte)1);
            item.setCreateTime(new Date());
            item.setLv((byte)2);
            item.setTitle(video.getString("title"));
            item.setVedioUrl(video.getString("url"));
            if(video.getInteger("num")!=null){
                item.setNum(video.getInteger("num"));
            }
            sectionMapper.insertSelective(item);
        }
    }
}
